package com.lkd.http.controller;

import com.lkd.entity.TaskEntity;
import com.lkd.exception.LogicException;
import com.lkd.http.vo.CancelTaskViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {
	@Autowired
	private TaskService taskService;

	/**
	 * 搜索工单
	 *
	 * @param pageIndex
	 * @param pageSize
	 * @param innerCode 设备编号
	 * @param userId    工单所属人Id
	 * @param taskCode  工单编号
	 * @param status    工单状态
	 * @param isRepair  是否是维修工单
	 * @return
	 */
	@GetMapping("/search")
	public Pager<TaskEntity> search(
			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Long pageIndex,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") Long pageSize,
			@RequestParam(value = "innerCode", required = false, defaultValue = "") String innerCode,
			@RequestParam(value = "userId", required = false, defaultValue = "") Integer userId,
			@RequestParam(value = "taskCode", required = false, defaultValue = "") String taskCode,
			@RequestParam(value = "status", required = false, defaultValue = "") Integer status,
			@RequestParam(value = "isRepair", required = false, defaultValue = "") Boolean isRepair,
			@RequestParam(value = "start", required = false, defaultValue = "") String start,
			@RequestParam(value = "end", required = false, defaultValue = "") String end) {
		return taskService.search(pageIndex, pageSize, innerCode, userId, taskCode, status, isRepair, start, end);
	}

	/**
	 * 新增工单
	 * @param vo
	 * @return
	 */
	@PostMapping("/create")
	public boolean create(@RequestBody TaskViewModel vo) {
		//获取当前登陆用户id
		Integer userId = getUserId();
		//setAssignorId  指派人id
		vo.setAssignorId(userId);
		return taskService.create(vo);
	}


	/**
	 * 接受工单
	 * @param taskId
	 * @return
	 */
	@GetMapping("/accept/{taskId}")
	public boolean accept( @PathVariable  String taskId){
		Long id = Long.valueOf(taskId);
		//判断工单执行人是否为当前登录用户
		TaskEntity task = taskService.getById(id);
		if(task.getUserId().intValue()!=getUserId().intValue() ){
			throw new LogicException("操作非法");
		}
		return taskService.accept( id );
	}


	/**
	 * 取消工单
	 * @param taskId
	 * @param cancelVm
	 * @return
	 */
	@PostMapping("/cancel/{taskId}")
	public boolean cancel( @PathVariable  String taskId ,@RequestBody CancelTaskViewModel cancelVm ){
		Long id = Long.valueOf(taskId);
		//判断工单执行人是否为当前登录用户
		TaskEntity task = taskService.getById(id);
		if(task.getUserId().intValue()!=getUserId().intValue() ){
			throw new LogicException("操作非法");
		}
		return taskService.cancelTask( id,cancelVm);
	}


	/**
	 * 完成工单
	 * @param taskId
	 * @return
	 */
	@GetMapping("/complete/{taskId}")
	public boolean complete(@PathVariable("taskId") String taskId    ){
		Long id = Long.valueOf(taskId);
		//判断工单执行人是否为当前登录用户
		TaskEntity task = taskService.getById(id);
		if(task.getUserId().intValue()!=getUserId().intValue() ){
			throw new LogicException("操作非法");
		}
		return taskService.completeTask(id);
	}



}